import { connect } from 'react-redux';

import * as effects from './effects';

interface MapState {
  [key: string]: (state: any) => any;
}

const mapToAction = () => ({
  commit: effects.commit
});

const mapToProps = (mapState: MapState = {}) => (
  (state: any) => {
    const stateToProps: MapState = {};
    Object.keys(mapState).forEach((key: string) => {
      stateToProps[key] = mapState[key](state);
    });
    return stateToProps;
  }
);

export default (mapState?: MapState): Function => {
  return connect(mapToProps(mapState), mapToAction);
}
